<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body>
<script>
description('Check stepUp() and stepDown() behavior for number type.');

var input = document.createElement('input');
var changeEventCounter = 0;
input.onchange = function() { ++changeEventCounter; };

function setInputAttributes(min, max, step, value) {
    input.min = min;
    input.max = max;
    input.step = step;
    input.value = value;
}

function stepUp(value, step, max, optionalStepCount) {
    setInputAttributes(null, max, step, value);
    if (typeof optionalStepCount != "undefined")
        input.stepUp(optionalStepCount);
    else
        input.stepUp();
    return input.value;
}

function stepDown(value, step, min, optionalStepCount) {
    setInputAttributes(min, null, step, value);
    if (typeof optionalStepCount != "undefined")
        input.stepDown(optionalStepCount);
    else
        input.stepDown();
    return input.value;
}

function stepUpExplicitBounds(min, max, step, value, stepCount) {
    setInputAttributes(min, max, step, value);
    if (typeof stepCount !== 'undefined')
        input.stepUp(stepCount);
    else
        input.stepUp();
    return input.value;
}

function stepDownExplicitBounds(min, max, step, value, stepCount) {
    setInputAttributes(min, max, step, value);
    if (typeof stepCount !== 'undefined')
        input.stepDown(stepCount);
    else
        input.stepDown();
    return input.value;
}

debug('Number type');
input.type = 'number';
debug('Invalid value');
shouldBeEqualToString('stepUp("", null, null)', '1');
shouldBeEqualToString('stepDown("", null, null)', '-1');
debug('Non-number arguments');
shouldBe('stepUp("0", null, null, "0")', '"0"');
shouldBe('stepDown("0", null, null, "0")', '"0"');
shouldBe('stepUp("0", null, null, "foo")', '"0"');
shouldBe('stepDown("0", null, null, "foo")', '"0"');
shouldBe('stepUp("0", null, null, null)', '"0"');
shouldBe('stepDown("0", null, null, null)', '"0"');
debug('Normal cases');
shouldBe('stepUp("0", null, null)', '"1"');
shouldBe('stepUp("1", null, null, 2)', '"3"');
shouldBe('stepUp("3", null, null, -1)', '"2"');
shouldBe('stepDown("2", null, null)', '"1"');
shouldBe('stepDown("1", null, null, 2)', '"-1"');
shouldBe('stepDown("-1", null, null, -1)', '"0"');
// According to Web IDL specification, 2147483648 here is converted to
// -2147483648.  So, stepDown(2147483648) is equivalent to stepUp(2147483648).
shouldBeEqualToString('stepDown("0", "1", null, 2147483648)', '2147483648');
debug('Extra arguments');
shouldBe('input.value = "0"; input.min = null; input.step = null; input.stepUp(1, 2); input.value', '"1"');
shouldBe('input.value = "1"; input.stepDown(1, 3); input.value', '"0"');
debug('Invalid step value');
shouldBe('stepUp("0", "foo", null)', '"1"');
shouldBe('stepUp("1", "0", null)', '"2"');
shouldBe('stepUp("2", "-1", null)', '"3"');
debug('Step=any');
shouldThrow('stepUp("0", "any", null)');
shouldThrow('stepDown("0", "any", null)');
debug('Step=any corner case');
shouldThrow('stepUpExplicitBounds("0", "100", "any", "1.5", "1")');
shouldThrow('stepDownExplicitBounds("0", "100", "any", "1.5", "1")');
debug('Overflow/underflow');
shouldBe('stepDown("1", "1", "0")', '"0"');
shouldBeEqualToString('stepDown("0", "1", "0")', '0');
shouldBeEqualToString('stepDown("1", "1", "0", 2)', '0');
shouldBeEqualToString('stepDown("1", "1.797693134862315e+308", "", 2)', '-1.797693134862315e+308');
shouldBeEqualToString('stepUp("-1", "1", "0")', '0');
shouldBeEqualToString('stepUp("0", "1", "0")', '0');
shouldBeEqualToString('stepUp("-1", "1", "0", 2)', '0');
shouldBeEqualToString('stepUp("1", "1.797693134862315e+308", "", 2)', '1.797693134862315e+308');
debug('stepDown()/stepUp() for stepMismatch values');
shouldBeEqualToString('stepUpExplicitBounds("0", "", "2", "1"); input.value', '2');
shouldBeEqualToString('stepUp("1", "2", "")', '2');
shouldBeEqualToString('input.stepDown(); input.value', '0');
shouldBeEqualToString('input.min = "0"; stepUp("9", "10", "", 9)', '90');
shouldBeEqualToString('stepDown("19", "10", "0")', '10');
shouldBeEqualToString('stepUp("89", "10", "99")', '90');
debug('Huge value and small step');
shouldBe('input.min = ""; stepUp("1e+308", "1", "", 999999)', '"1e+308"');
shouldBe('input.max = ""; stepDown("1e+308", "1", "", 999999)', '"1e+308"');
debug('Fractional numbers');
shouldBe('input.min = ""; stepUp("0", "0.33333333333333333", "", 3)', '"1"');
shouldBe('stepUp("1", "0.1", "", 10)', '"2"');
shouldBe('input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.stepUp(); input.value', '"3"');
shouldBe('input.min = "0"; stepUp("0", "0.003921568627450980", "1", 255)', '"1"');
shouldBe('for (var i = 0; i < 255; i++) { input.stepDown(); }; input.value', '"0"');
debug('Rounding');
shouldBe('stepUp("5.005", "0.005", "", 2)', '"5.015"');
shouldBe('stepUp("5.005", "0.005", "", 11)', '"5.06"');
shouldBe('stepUp("5.005", "0.005", "", 12)', '"5.065"');
shouldBe('stepUpExplicitBounds("4", "9", "0.005", "5.005", 2)', '"5.015"');
shouldBe('stepUpExplicitBounds("4", "9", "0.005", "5.005", 11)', '"5.06"');
shouldBe('stepUpExplicitBounds("4", "9", "0.005", "5.005", 12)', '"5.065"');

debug('');
debug('This test should not dispatch any |change| events.');
shouldBe('changeEventCounter', '0');
</script>
</body>
</html>
